<template>
  <div>
    <div class="gva-search-box">
      <el-form :inline="true" :model="searchInfo" class="demo-form-inline" @keyup.enter="onSubmit">
        <el-form-item label="故障对应的工艺设计文件号">
          <el-input v-model="searchInfo.process_design_document_number" placeholder="搜索条件" />
 
         </el-form-item>
         <el-form-item label="ATA章节（一级）">
          <el-input v-model="searchInfo.ata_chapter_level1" placeholder="搜索条件" />
 
         </el-form-item>
         <el-form-item label="ATA章节（二级）">
          <el-input v-model="searchInfo.ata_chapter_level2" placeholder="搜索条件" />
 
         </el-form-item>
         <el-form-item label="故障描述">
          <el-input v-model="searchInfo.fault_description" placeholder="搜索条件" />
 
         </el-form-item>
         <el-form-item label="ATA章标题名称">
          <el-input v-model="searchInfo.ata_chapter_name" placeholder="搜索条件" />
 
         </el-form-item>
         <el-form-item label="故障对应的工艺设计文件名">
          <el-input v-model="searchInfo.process_design_document_name" placeholder="搜索条件" />
 
         </el-form-item>
        <!-- <el-form-item label="创建时间">
      <el-date-picker v-model="searchInfo.startCreatedAt" type="datetime" placeholder="开始时间"></el-date-picker>
       —
      <el-date-picker v-model="searchInfo.endCreatedAt" type="datetime" placeholder="结束时间"></el-date-picker>
      </el-form-item>
        <el-form-item label="飞机型号MODEL">
         <el-input v-model="searchInfo.aircraft_model" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="编号">
         <el-input v-model="searchInfo.number" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="排故日期">
            
           <el-date-picker v-model="searchInfo.troubleshooting_date" type="datetime" placeholder="搜索条件"></el-date-picker>

        </el-form-item>
        <el-form-item label="制造序列号">
         <el-input v-model="searchInfo.manufacturing_serial_number" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="故障对应的工艺设计文件号">
         <el-input v-model="searchInfo.process_design_document_number" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="故障对应的工艺设计文件名">
         <el-input v-model="searchInfo.process_design_document_name" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="负责工艺员">
         <el-input v-model="searchInfo.responsible_craftman" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="负责工艺员工号">
         <el-input v-model="searchInfo.responsible_craftman_number" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="故障对应的工程文件">
         <el-input v-model="searchInfo.fault_project_file" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="ATA章节（一级）">
         <el-input v-model="searchInfo.ata_chapter_level1" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="ATA章节（二级）">
         <el-input v-model="searchInfo.ata_chapter_level2" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="ATA章标题名称">
         <el-input v-model="searchInfo.ata_chapter_name" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="设计要求的操作和响应">
         <el-input v-model="searchInfo.operation_and_response_to_design_requirements" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="故障名称">
         <el-input v-model="searchInfo.fault_name" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="故障描述">
         <el-input v-model="searchInfo.fault_description" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="故障原理">
         <el-input v-model="searchInfo.fault_principle" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="排故思路">
         <el-input v-model="searchInfo.troubleshooting_ideas" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="DISR号">
         <el-input v-model="searchInfo.dISR_number" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="操作描述1">
         <el-input v-model="searchInfo.operation_description1" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="操作描述2">
         <el-input v-model="searchInfo.operation_description2" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="操作描述3">
         <el-input v-model="searchInfo.operation_description3" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="操作描述4">
         <el-input v-model="searchInfo.operation_description4" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="操作描述5">
         <el-input v-model="searchInfo.operation_description5" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="故障树上传">
         <el-input v-model="searchInfo.upload_fault_tree" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="结论">
         <el-input v-model="searchInfo.conclusion" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="完成时间">
            
           <el-date-picker v-model="searchInfo.complete_date" type="datetime" placeholder="搜索条件"></el-date-picker>

        </el-form-item>
        <el-form-item label="纠正措施">
         <el-input v-model="searchInfo.corrective_ction" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="人工操作">
         <el-input v-model="searchInfo.line_fault_operation" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="连接器(到)">
         <el-input v-model="searchInfo.line_fault_connector_to" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="针孔号（到）">
         <el-input v-model="searchInfo.line_fault_pinhole_to" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="测试结果">
         <el-input v-model="searchInfo.line_fault_result" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="章节">
         <el-input v-model="searchInfo.line_fault_chapter" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="前置条件-文件号">
         <el-input v-model="searchInfo.pre_factor_number" placeholder="搜索条件" />

        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="search" @click="onSubmit">查询</el-button>
          <el-button icon="refresh" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="gva-table-box">
        <div class="gva-btn-list">
            <el-button type="primary" icon="plus" @click="AddForm">新增</el-button>
            <el-popover v-model:visible="deleteVisible" placement="top" width="160">
            <p>确定要删除吗？</p>
            <div style="text-align: right; margin-top: 8px;">
                <el-button type="primary" link @click="deleteVisible = false">取消</el-button>
                <el-button type="primary" @click="onDelete">确定</el-button>
            </div>
            <template #reference>
                <el-button icon="delete" style="margin-left: 10px;" :disabled="!multipleSelection.length" @click="deleteVisible = true">删除</el-button>
            </template>
            </el-popover>
        </div>
        <el-table
        ref="multipleTable"
        style="width: 100%"
        tooltip-effect="dark"
        :data="tableData"
        row-key="ID"
        @selection-change="handleSelectionChange"
        >
        <el-table-column type="selection" width="55" />
<!--  添加一行自然序号-->
        <el-table-column align="left" label="序号" width="80">
            <template #default="scope">{{ scope.$index + 1 }}</template>
        </el-table-column>
        <!-- <el-table-column align="left" label="日期" width="180">
            <template #default="scope">{{ formatDate(scope.row.CreatedAt) }}</template>
        </el-table-column> -->
        <!-- <el-table-column align="left" label="飞机型号MODEL" prop="aircraft_model" width="120" />
        <el-table-column align="left" label="编号" prop="number" width="120" />
         <el-table-column align="left" label="排故日期" width="180">
            <template #default="scope">{{ formatDate(scope.row.troubleshooting_date) }}</template>
         </el-table-column> -->
        <el-table-column align="left" label="制造序列号（架次）" prop="manufacturing_serial_number" width="150" />
        <!-- <el-table-column align="left" label="故障对应的工艺设计文件名" prop="process_design_document_name" width="120" />
        <el-table-column align="left" label="负责工艺员" prop="responsible_craftman" width="120" />
        <el-table-column align="left" label="负责工艺员工号" prop="responsible_craftman_number" width="120" />
        <el-table-column align="left" label="故障对应的工程文件" prop="fault_project_file" width="120" /> -->
        <el-table-column align="left" label="ATA章节（一级）" prop="ata_chapter_level1" width="150" />
        <el-table-column align="left" label="ATA章节（二级）" prop="ata_chapter_level2" width="150" />
        <el-table-column align="left" label="ATA章标题" prop="ata_chapter_name" width="120" />
        <el-table-column align="left" label="故障对应的工艺设计文件号" prop="process_design_document_number" width="250" />

        <!-- <el-table-column align="left" label="设计要求的操作和响应" prop="operation_and_response_to_design_requirements" width="120" /> -->
        <el-table-column align="left" label="故障名称" prop="fault_name" width="120" />
        <el-table-column align="left" label="故障描述" prop="fault_description" width="120" />
        <!-- <el-table-column align="left" label="故障原理" prop="fault_principle" width="120" />
        <el-table-column align="left" label="排故思路" prop="troubleshooting_ideas" width="120" />
        <el-table-column align="left" label="故障隔离步骤" prop="fault_isolation_steps" width="120" />
        <el-table-column align="left" label="参考资料" prop="reference" width="120" />
        <el-table-column align="left" label="DISR号" prop="dISR_number" width="120" />
        <el-table-column align="left" label="序号1" prop="serial_number1" width="120" />
        <el-table-column align="left" label="操作描述1" prop="operation_description1" width="120" />
        <el-table-column align="left" label="是否需要质量检查1" prop="is_quality_inspection_required1" width="120">
            <template #default="scope">{{ formatBoolean(scope.row.is_quality_inspection_required1) }}</template>
        </el-table-column>
        <el-table-column align="left" label="序号2" prop="serial_number2" width="120" />
        <el-table-column align="left" label="操作描述2" prop="operation_description2" width="120" />
        <el-table-column align="left" label="是否需要质量测查2" prop="is_quality_inspection_required2" width="120">
            <template #default="scope">{{ formatBoolean(scope.row.is_quality_inspection_required2) }}</template>
        </el-table-column>
        <el-table-column align="left" label="序号3" prop="serial_number3" width="120" />
        <el-table-column align="left" label="操作描述3" prop="operation_description3" width="120" />
        <el-table-column align="left" label="是否需要质量检测3" prop="is_quality_inspection_required3" width="120">
            <template #default="scope">{{ formatBoolean(scope.row.is_quality_inspection_required3) }}</template>
        </el-table-column>
        <el-table-column align="left" label="序号4" prop="serial_number4" width="120" />
        <el-table-column align="left" label="操作描述4" prop="operation_description4" width="120" />
        <el-table-column align="left" label="是否需要质量检查4" prop="is_quality_inspection_required4" width="120">
            <template #default="scope">{{ formatBoolean(scope.row.is_quality_inspection_required4) }}</template>
        </el-table-column>
        <el-table-column align="left" label="序号5" prop="serial_number5" width="120" />
        <el-table-column align="left" label="操作描述5" prop="operation_description5" width="120" />
        <el-table-column align="left" label="是否需要质量检测5" prop="is_quality_inspection_required5" width="120">
            <template #default="scope">{{ formatBoolean(scope.row.is_quality_inspection_required5) }}</template>
        </el-table-column>
        <el-table-column align="left" label="纸质排故单上传" prop="upload_troubleshooting_form" width="120" />
        <el-table-column align="left" label="附件上传" prop="upload_attachment" width="120" />
        <el-table-column align="left" label="故障树上传" prop="upload_fault_tree" width="120" />
        <el-table-column align="left" label="结论" prop="conclusion" width="120" />
         <el-table-column align="left" label="完成时间" width="180">
            <template #default="scope">{{ formatDate(scope.row.complete_date) }}</template>
         </el-table-column>
        <el-table-column align="left" label="是否通过" prop="is_passed" width="120">
            <template #default="scope">{{ formatBoolean(scope.row.is_passed) }}</template>
        </el-table-column>
        <el-table-column align="left" label="DI-SR/FRR号" prop="frr_number" width="120" />
        <el-table-column align="left" label="纠正措施" prop="corrective_ction" width="120" />
        <el-table-column align="left" label="线路故障" prop="is_line_fault" width="120">
            <template #default="scope">{{ formatBoolean(scope.row.is_line_fault) }}</template>
        </el-table-column>
        <el-table-column align="left" label="连接器(从)" prop="line_fault_connector_from" width="120" />
        <el-table-column align="left" label="针孔号(从)" prop="line_fault_pinhole_from" width="120" />
        <el-table-column align="left" label="人工操作" prop="line_fault_operation" width="120" />
        <el-table-column align="left" label="连接器(到)" prop="line_fault_connector_to" width="120" />
        <el-table-column align="left" label="针孔号（到）" prop="line_fault_pinhole_to" width="120" />
        <el-table-column align="left" label="测试结果" prop="line_fault_result" width="120" />
        <el-table-column align="left" label="章节" prop="line_fault_chapter" width="120" />
        <el-table-column align="left" label="备注" prop="line_fault_note" width="120" />
        <el-table-column align="left" label="设备故障" prop="is_equipment_fault" width="120">
            <template #default="scope">{{ formatBoolean(scope.row.is_equipment_fault) }}</template>
        </el-table-column>
        <el-table-column align="left" label="设备故障-设备编号" prop="equipment_fault_number" width="120" />
        <el-table-column align="left" label="设备故障-设备名称" prop="equipment_fault_name" width="120" />
        <el-table-column align="left" label="设备故障-供应商零件标准号" prop="equipment_fault_component_number" width="120" />
        <el-table-column align="left" label="设备故障-设备安装图号" prop="equipment_fault_install_number" width="120" />
        <el-table-column align="left" label="设备故障-章节" prop="equipment_fault_chapter" width="120" />
        <el-table-column align="left" label="设备故障-备注" prop="equipment_fault_note" width="120" />
        <el-table-column align="left" label="前置条件" prop="is_pre_factor" width="120">
            <template #default="scope">{{ formatBoolean(scope.row.is_pre_factor) }}</template>
        </el-table-column>
        <el-table-column align="left" label="前置条件-文件号" prop="pre_factor_number" width="120" />
        <el-table-column align="left" label="前置条件-文件名称" prop="pre_factor_name" width="120" />
        <el-table-column align="left" label="前置条件-闲置内容" prop="pre_factor_content" width="120" />
        <el-table-column align="left" label="前置条件-备注" prop="pre_factor_note" width="120" />
        <el-table-column align="left" label="人为因素" prop="is_human_factor" width="120">
            <template #default="scope">{{ formatBoolean(scope.row.is_human_factor) }}</template>
        </el-table-column>
        <el-table-column align="left" label="人为因素具体内容" prop="human_factor_context" width="120" /> -->
        <el-table-column align="left" label="操作">
            <template #default="scope">
            <el-button type="primary" link icon="edit" class="table-button" @click="updateMergeForm_structFunc(scope.row)">变更</el-button>
            <el-button type="primary" link icon="delete" @click="deleteRow(scope.row)">删除</el-button>
            </template>
        </el-table-column>
        </el-table>
        <div class="gva-pagination">
            <el-pagination
            layout="total, sizes, prev, pager, next, jumper"
            :current-page="page"
            :page-size="pageSize"
            :page-sizes="[10, 30, 50, 100]"
            :total="total"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
            />
        </div>
    </div>

  </div>
</template>

<script>
export default {
  name: 'MergeForm_struct'
}
</script>

<script setup>
import {
  createMergeForm_struct,
  deleteMergeForm_struct,
  deleteMergeForm_structByIds,
  updateMergeForm_struct,
  findMergeForm_struct,
  getMergeForm_structList
} from '@/api/sys_mergeForm'

// 全量引入格式化工具 请按需保留
import { getDictFunc, formatDate, formatBoolean, filterDict } from '@/utils/format'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
// 自动化生成的字典（可能为空）以及字段
const formData = ref({
        aircraft_model: '',
        number: '',
        troubleshooting_date: new Date(),
        manufacturing_serial_number: '',
        process_design_document_number: '',
        process_design_document_name: '',
        responsible_craftman: '',
        responsible_craftman_number: '',
        fault_project_file: '',
        ata_chapter_level1: '',
        ata_chapter_level2: '',
        ata_chapter_name: '',
        operation_and_response_to_design_requirements: '',
        fault_name: '',
        fault_description: '',
        fault_principle: '',
        troubleshooting_ideas: '',
        fault_isolation_steps: '',
        reference: '',
        dISR_number: '',
        serial_number1: 0,
        operation_description1: '',
        is_quality_inspection_required1: false,
        serial_number2: 0,
        operation_description2: '',
        is_quality_inspection_required2: false,
        serial_number3: 0,
        operation_description3: '',
        is_quality_inspection_required3: false,
        serial_number4: 0,
        operation_description4: '',
        is_quality_inspection_required4: false,
        serial_number5: 0,
        operation_description5: '',
        is_quality_inspection_required5: false,
        upload_troubleshooting_form: '',
        upload_attachment: '',
        upload_fault_tree: '',
        conclusion: '',
        complete_date: new Date(),
        is_passed: false,
        frr_number: '',
        corrective_ction: '',
        is_line_fault: false,
        line_fault_connector_from: '',
        line_fault_pinhole_from: '',
        line_fault_operation: '',
        line_fault_connector_to: '',
        line_fault_pinhole_to: '',
        line_fault_result: '',
        line_fault_chapter: '',
        line_fault_note: '',
        is_equipment_fault: false,
        equipment_fault_number: '',
        equipment_fault_name: '',
        equipment_fault_component_number: '',
        equipment_fault_install_number: '',
        equipment_fault_chapter: '',
        equipment_fault_note: '',
        is_pre_factor: false,
        pre_factor_number: '',
        pre_factor_name: '',
        pre_factor_content: '',
        pre_factor_note: '',
        is_human_factor: false,
        human_factor_context: '',
        })

// 验证规则
const rule = reactive({
               aircraft_model : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               number : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               troubleshooting_date : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               manufacturing_serial_number : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               process_design_document_number : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               responsible_craftman : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               responsible_craftman_number : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               ata_chapter_level1 : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               ata_chapter_level2 : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               fault_description : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               conclusion : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
})

const elFormRef = ref()


// =========== 表格控制部分 ===========
const page = ref(1)
const total = ref(0)
const pageSize = ref(10)
const tableData = ref([])
const searchInfo = ref({})

// 重置
const onReset = () => {
  searchInfo.value = {}
  getTableData()
}

// 搜索
const onSubmit = () => {
  page.value = 1
  pageSize.value = 10
  if (searchInfo.value.is_quality_inspection_required1 === ""){
      searchInfo.value.is_quality_inspection_required1=null
  }
  if (searchInfo.value.is_quality_inspection_required2 === ""){
      searchInfo.value.is_quality_inspection_required2=null
  }
  if (searchInfo.value.is_quality_inspection_required3 === ""){
      searchInfo.value.is_quality_inspection_required3=null
  }
  if (searchInfo.value.is_quality_inspection_required4 === ""){
      searchInfo.value.is_quality_inspection_required4=null
  }
  if (searchInfo.value.is_quality_inspection_required5 === ""){
      searchInfo.value.is_quality_inspection_required5=null
  }
  if (searchInfo.value.is_passed === ""){
      searchInfo.value.is_passed=null
  }
  if (searchInfo.value.is_line_fault === ""){
      searchInfo.value.is_line_fault=null
  }
  if (searchInfo.value.is_equipment_fault === ""){
      searchInfo.value.is_equipment_fault=null
  }
  if (searchInfo.value.is_pre_factor === ""){
      searchInfo.value.is_pre_factor=null
  }
  if (searchInfo.value.is_human_factor === ""){
      searchInfo.value.is_human_factor=null
  }
  getTableData()
}

// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  getTableData()
}

// 修改页面容量
const handleCurrentChange = (val) => {
  page.value = val
  getTableData()
}

// 查询
const getTableData = async() => {
  const table = await getMergeForm_structList({ page: page.value, pageSize: pageSize.value, ...searchInfo.value })
  if (table.code === 0) {
    tableData.value = table.data.list
    total.value = table.data.total
    page.value = table.data.page
    pageSize.value = table.data.pageSize
  }
}

getTableData()

// ============== 表格控制部分结束 ===============

// 获取需要的字典 可能为空 按需保留
const setOptions = async () =>{
}

// 获取需要的字典 可能为空 按需保留
setOptions()


// 多选数据
const multipleSelection = ref([])
// 多选
const handleSelectionChange = (val) => {
    multipleSelection.value = val
}

// 删除行
const deleteRow = (row) => {
    ElMessageBox.confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(() => {
            deleteMergeForm_structFunc(row)
        })
    }


// 批量删除控制标记
const deleteVisible = ref(false)

// 多选删除
const onDelete = async() => {
      const ids = []
      if (multipleSelection.value.length === 0) {
        ElMessage({
          type: 'warning',
          message: '请选择要删除的数据'
        })
        return
      }
      multipleSelection.value &&
        multipleSelection.value.map(item => {
          ids.push(item.ID)
        })
      const res = await deleteMergeForm_structByIds({ ids })
      if (res.code === 0) {
        ElMessage({
          type: 'success',
          message: '删除成功'
        })
        if (tableData.value.length === ids.length && page.value > 1) {
          page.value--
        }
        deleteVisible.value = false
        getTableData()
      }
    }

// 行为控制标记（弹窗内部需要增还是改）
const type = ref('')

// 更新行
const updateMergeForm_structFunc = async(row) => {
  router.push({
      name:"mergeForm",
      query:{
        id:row.ID
      }
    })

}


// 删除行
const deleteMergeForm_structFunc = async (row) => {
    const res = await deleteMergeForm_struct({ ID: row.ID })
    if (res.code === 0) {
        ElMessage({
                type: 'success',
                message: '删除成功'
            })
            if (tableData.value.length === 1 && page.value > 1) {
            page.value--
        }
        getTableData()
    }
}


// 打开弹窗
const AddForm = () => {
  router.push({
      name:"mergeForm"
    })
}


</script>

<style>
</style>
